package com.steptime.algorithm;

import org.junit.Test;

import java.util.ArrayDeque;
import java.util.HashMap;

public class ArrDemo {

    @Test
    public void removeDuplicatesTest(){
        System.out.println(getTrueStr("(]"));

    }

    public boolean getTrueStr(String str){
        HashMap<Character,Character> hashMap = new HashMap();
        hashMap.put(')', '(');
        hashMap.put('}', '{');
        hashMap.put(']', '[');
        ArrayDeque deque = new ArrayDeque();
        for (int i=0;i<str.length();i++) {
            char ch = str.charAt(i);
            if (hashMap.containsKey(ch)) { //需要匹配
                if (str.isEmpty() || deque.peekFirst() != hashMap.get(ch)) {
                    return false;
                }
                deque.pop();
            }else{
                deque.push(ch);
            }
        }
        return deque.isEmpty();
    }
}